package com.example.qx.faxiana;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.qx.R;
import com.example.qx.adapter.qxqadapter;
import com.example.qx.database.data;
import com.example.qx.lei.bjtx;
import com.example.qx.lei.cos;
import com.example.qx.lei.itemqxq;
import com.example.qx.lei.leilianxr;
import com.example.qx.lei.tianjianqxq;
import com.example.qx.lei.zhangmi;
import com.example.qx.zy;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;

public class qxq extends AppCompatActivity {

    private Button button,butsz,butfh;

    private RecyclerView recyclerView;
    private List<itemqxq> list=new ArrayList<>();
    private zhangmi zhangmi= com.example.qx.lei.zhangmi.getInstance();
    private data data=new data();
   private cos cos=new cos();
    public qxq() {}
    @SuppressLint("SuspiciousIndentation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qxq);
        butsz=findViewById(R.id.shezhi);
        butfh=findViewById(R.id.fanhui);
        button=findViewById(R.id.tianjiaqxq);
        ImageView bj = findViewById(R.id.beijing1);
        ImageView tx = findViewById(R.id.touxiang1);
        final Connection[] connection = new Connection[1];
        final leilianxr[] leilianxr = new leilianxr[1];
        final List<Integer>[] listzhanghao = new List[]{null};
        CountDownLatch latch=new CountDownLatch(1);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {Class.forName("com.mysql.jdbc.Driver").newInstance();}
                catch (IllegalAccessException | ClassNotFoundException |
                       InstantiationException e) {
                    throw new RuntimeException(e);}
                try {
                    connection[0] = DriverManager.getConnection("jdbc:mysql://sh-cdb-9761pjk2.sql.tencentcdb.com:29081/youqi","root","asd123456");
                    leilianxr[0] = data.getbjtx(zhangmi.getZhang(),connection[0]);
                    if(data.getallqxq(zhangmi.getZhang(),connection[0]).size()>0)
                        list.addAll(data.getallqxq(zhangmi.getZhang(),connection[0]));

                    listzhanghao[0] = data.getpy( zhangmi.getZhang(),1,connection[0]);
                    for(int d = 0; d< listzhanghao[0].size(); d++)
                    {
                        if(data.getallqxq(listzhanghao[0].get(d),connection[0]).size()>0)
                                list.addAll(data.getallqxq(listzhanghao[0].get(d),connection[0]));
                    }
                } catch (SQLException e) {throw new RuntimeException(e);}
                latch.countDown();
            }
        }).start();
        try{
            latch.await();
            connection[0].close();
        }
        catch (InterruptedException | SQLException e)
        {e.printStackTrace();}
         butfh.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 Intent intent=new Intent(qxq.this, zy.class);
                 startActivity(intent);
             }
         });

        ContentResolver contentResolver=getContentResolver();
           String z=contentResolver.getType(Uri.parse(leilianxr[0].getBeijing()));
        if(!Objects.equals(leilianxr[0].getBeijing(), "s")&&(leilianxr[0].getBeijing()!=null)){
              if(z.startsWith("image/"))
             {        File filee=this.getDir("youqishuju",this.MODE_PRIVATE);
                 String abc= filee.getAbsolutePath();
                 File filel=new File(abc+"/"+String.valueOf(zhangmi.getZhang()+"bj"));
                    if(!filel.exists())
                    cos.get(String.valueOf(zhangmi.getZhang()+"bj"),getApplication());
                 Bitmap bitmap= BitmapFactory.decodeFile(abc+"/"+String.valueOf(zhangmi.getZhang()+"bj"));
                 bj.setImageBitmap(bitmap);}}
            String g=contentResolver.getType(Uri.parse(leilianxr[0].getTouxiang()));

              if(!Objects.equals(leilianxr[0].getTouxiang(), "s")&&(leilianxr[0].getTouxiang()!=null)){
                  if(g.startsWith("image/")) {
                      File filee=this.getDir("youqishuju",this.MODE_PRIVATE);
                      String abc= filee.getAbsolutePath();
                      File filel=new File(abc+"/"+String.valueOf(zhangmi.getZhang()+"tx"));
                      if(!filel.exists()) {
                          cos.get(String.valueOf(zhangmi.getZhang() + "tx"), getApplication());
                      }
                Bitmap bitmap1=BitmapFactory.decodeFile(abc+"/"+String.valueOf(zhangmi.getZhang()+"tx"));
                tx.setImageBitmap(bitmap1);

        } }
        butsz.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {

                 Intent intent=new Intent(qxq.this,szbjtx.class);
                 startActivity(intent);
             }
         });
         button.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                Intent intent=new Intent(qxq.this,tianjiaqxqa.class);
                 startActivity(intent);
             }
         });

          if(list.size()>0){
              recyclerView=findViewById(R.id.fabiaoqxq);
              recyclerView.setLayoutManager(new LinearLayoutManager(this));
              qxqadapter qxqadapter= null;
              try {
                  qxqadapter = new qxqadapter(list,this);
              } catch (SQLException | ClassNotFoundException | IllegalAccessException |
                       InstantiationException e) {
                  throw new RuntimeException(e);
              }
              recyclerView.setAdapter(qxqadapter);

          }

    }
    }
